package com.example.zhinengjiaotong;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.JsonObject;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {

    private TextView textView;
    private Button btnSetting;
    private EditText editUser;
    private EditText editPassword;
    private CheckBox chkMainPassword;
    private CheckBox chkMainName;
    private Button btnLogin;
    private Button btnZhuce;
    Button login,back;
    long exittime;
    EditText username,pwd;

    private String url = "http://121.9.253.237:8888/traffic/api/v2/";
    OkHttpClient okHttpClient = new OkHttpClient();
    private SharedPreferences sp;

    @Override
    protected void onResume() {
        super.onResume();
        boolean isSavePwd = sp.getBoolean("isSavsPwd", false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        sp = getSharedPreferences("login", MODE_PRIVATE);
        chkMainName.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("chkName",b);
                editor.commit();
                if (b) {
                    chkMainName.setChecked(true);
                    chkMainName.setEnabled(false);
                }else {
                    chkMainName.setEnabled(true);
                }
            }

        });
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                String  userName=editUser.getText().toString().trim();
                String  password=editPassword.getText().toString().trim();

                if (userName.equals("admin")&&password.equals("123456")) {
                    if (chkMainName.isChecked()) {
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("userName", userName);
                        editor.putString("password",password);
                        editor.commit();
                    }
                    Intent intent = new Intent(com.example.zhinengjiaotong.LoginActivity.this, MainActivity1.class);
                    intent.putExtra("passWord", editPassword.getText().toString().trim());
                    Bundle bundle = new Bundle();
                    bundle.putString("passWord", editPassword.getText().toString().trim());
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else {
                    AlertDialog.Builder builder=new AlertDialog.Builder(com.example.zhinengjiaotong.LoginActivity.this);
                    builder.setMessage("请输入的用户名或密码错误！");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            com.example.zhinengjiaotong.LoginActivity.this.finish();
                        }
                    });
                    AlertDialog dialog =builder.create();
                    dialog.show();
                    if (chkMainPassword.isChecked()) {
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("password", password);
                        editor.commit();
                    }
                }
            }
        });
    }

    protected void onStart() {
        super.onStart();
        boolean chkName = sp.getBoolean("chkName", false);
        chkMainName.setChecked(chkName);
        boolean chkPassword = sp.getBoolean("chkPassword", false);
        chkMainPassword.setChecked(chkPassword);
        if (chkName) {
            editUser.setText(sp.getString("userName", null));
        }
        if (chkPassword) {
            editPassword.setText(sp.getString("password", null));
        }
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName = editUser.getText().toString().trim();
                String password = editPassword.getText().toString().trim();
                new  Thread(new Runnable() {
                    @Override
                    public void run() {
                        JsonObject jsonObject=new JsonObject();
                        jsonObject.addProperty("UserName",userName);
                        jsonObject.addProperty("UserPassword",password);
                        String json=jsonObject.toString();
                        MediaType mediaType=MediaType.Companion.parse("application/json;charset=utf-8");
                        RequestBody requestBody=RequestBody.create(json,mediaType);
                        Request request=new Request.Builder()
                                .url(url)
                                .post(requestBody)
                                .build();
                        Call call=okHttpClient.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {

                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                String  result=response.body().string();
                                Log.i("TAG","onResponse:"+result);

                            }
                        });
                    }

                }).start();
            }
        });

    }


    private void initView() {
        textView = (TextView) findViewById(R.id.textView);
        btnSetting = (Button) findViewById(R.id.btn_Setting);
        editUser = (EditText) findViewById(R.id.edit_user);
        editPassword = (EditText) findViewById(R.id.edit_password);
        chkMainPassword = (CheckBox) findViewById(R.id.chk_Mainpassword);
        chkMainName = (CheckBox) findViewById(R.id.chk_MainName);
        btnLogin = (Button) findViewById(R.id.btn_login);
        btnZhuce = (Button) findViewById(R.id.btn_zhuce);
    }
}